//
//  CollectViewController.m
//  ShangPiBao
//
//  Created by chirui on 2017/10/25.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "CollectViewController.h"
#import "MyCollectTableViewCell.h"
#import "DBManager.h"
#import "DBModel.h"
#import "CompanyMainDetailsVController.h"

@interface CollectViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    UITableView * _tableView;
    NSMutableArray * _dataArray;
}

@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArray = @[].mutableCopy;
    TitleView(@"我的收藏");
    _dataArray = [[DBManager sharedManager] fetchAllData];
    
    NSLog(@"_DataRRAY = %@",_dataArray);
    
//    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_nav_return" target:self action:@selector(back)];
    
    [self creatTableView];
}

-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)creatTableView {
    
    _tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView * view =[[UIView alloc]init];
    _tableView.tableFooterView = view;
    [self.view addSubview:_tableView];
    
    [_tableView registerNib:[UINib nibWithNibName:[MyCollectTableViewCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[MyCollectTableViewCell reuseIdentifier]];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _dataArray.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 120;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DBModel * model = _dataArray[indexPath.row];
    MyCollectTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:[MyCollectTableViewCell reuseIdentifier]];
    cell.companyName.text = model.name;
    cell.timeLabel.text = model.time;
    cell.deledateBtn.tag = 100+indexPath.row;
    [cell.deledateBtn addTarget:self action:@selector(deledateBtn:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)deledateBtn:(UIButton *)btn{
    DBModel * model = _dataArray[btn.tag - 100];
    
    NSInteger index = btn.tag - 100;
    NSLog(@"model.id = %@" , model.appId);
    [[DBManager sharedManager] deleteDataByModel:_dataArray[index]]; // 从数据库中删除
     [_dataArray removeObjectAtIndex:index]; //从数据中删除
    
    [_tableView reloadData];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    DBModel * model = _dataArray[indexPath.row];
    CompanyMainDetailsVController * company =[[CompanyMainDetailsVController alloc]init];
    company.companyId = model.appId;
    company.companyNameStr = model.name;
    [self.navigationController pushViewController:company animated:YES];
    
}

@end
